// 固定租金
<template>
  <div class="padding-right-15">
    <asset-table v-if="contractType == 'ZL'" ref="rent" :isEdit="isEdit" :chose-project-key="currentProjectKey" :list="contractInfo.rentCycleList" :contract-info="contractInfo" @setAssetList="getSelfAssetsList"></asset-table>
    <div class="font-size-18 font-weight-700">合同费用约定</div>
    <div class="padding-left-20 margin-bottom-15">
      <rent-table ref="fixed" page-name="fixedRentList" v-if="provideObj.rentMode == 'fixed' || provideObj.rentMode == 'fixed_share' || contractType == 'WRSB' || provideObj.rentMode == 'get_high'" :isEdit="isEdit" title="固定租金" :chose-project-key="currentProjectKey" page-type="rent" :self-assets-list="selfAssetsList" :option-obj="optionObj" :list="contractInfo.fixedRentList"></rent-table>
      <rent-table ref="share" page-name="shareRentList" v-if="contractType == 'ZL' && (provideObj.rentMode == 'share' || provideObj.rentMode == 'fixed_share' || provideObj.rentMode == 'get_high')" :isEdit="isEdit" title="分成租金" page-type="shareRent" :chose-project-key="currentProjectKey" :self-assets-list="selfAssetsList" :option-obj="optionObj" :list="contractInfo.shareRentList"></rent-table>
      <rent-table ref="manage" page-name="manageRentList" v-if="contractType == 'ZL'" :isEdit="isEdit" title="其他周期性付款费用项（管理费）" page-type="manage" :chose-project-key="currentProjectKey" :self-assets-list="selfAssetsList" :option-obj="optionObj" :list="contractInfo.manageRentList"></rent-table>
      <discount-table ref="discount" page-name="discountRentList" v-if="contractType == 'ZL'" :isEdit="isEdit" :chose-project-key="currentProjectKey" :self-assets-list="selfAssetsList" :discountTypeList="optionObj.discountType" :list="contractInfo.discountRentList"></discount-table>
      <div v-if="contractType == 'ZL' && isEdit">
        <span class="font-size-16 font-weight-700 padding-right-20">其他非周期性付款费用项</span>
        <el-button type="text" @click="addOtherCash">添加</el-button>
      </div>
      <guarantee-table ref="performance" page-name="performanceDepositList" :isEdit="isEdit" v-if="checkOtherList.indexOf('guarantee') > -1 && contractType == 'ZL'" :chose-project-key="currentProjectKey" :self-assets-list="selfAssetsList" :list="contractInfo.performanceDepositList"></guarantee-table>
      <other-cost-table ref="other" page-name="otherRentList" :isEdit="isEdit" v-if="checkOtherList.indexOf('other') > -1 && contractType == 'ZL'" :chose-project-key="currentProjectKey" :self-assets-list="selfAssetsList" :list="contractInfo.otherRentList"></other-cost-table>
    </div>

    <el-dialog title="请选择添加的费项" :visible.sync="otherCashDialog" width="600px" :close-on-click-modal="false">
      <div>
        <el-checkbox v-model="item.checked" v-for="item in otherCashList" :key="item.key">{{ item.label }}</el-checkbox>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="otherCashDialog = false">取 消</el-button>
        <el-button type="primary" @click="confirmOtherCash">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import rentTable from '../../../components/contract/rentTable.vue';
  import assetTable from '../../../components/contract/assetTable.vue';
  import discountTable from '../../../components/contract/discountTable.vue';
  import guaranteeTable from '../../../components/contract/guaranteeTable.vue';
  import otherCostTable from '../../../components/contract/otherCostTable.vue';
  import { apiConstant } from '../../../api/contract';
  import lodash from 'lodash';

  export default {
    props: {
      contractType: {
        type: String,
        default: '',
      },
      //基本信息中选择的项目key
      choseProjectKey: {
        type: String,
        default: '',
      },
      contractInfo: {
        type: Object,
        default: () => {
          return {};
        },
      },
      dictoryList: {
        type: Array,
        default: () => {
          return [];
        },
      },
      isFirst: {
        type: Boolean,
        default: true,
      },
    },

    inject: ['provideObj'],

    data() {
      return {
        tableList: [{}],
        stageList: [], //阶段
        assetList: [], //资产
        unitPriceList: [], //单位(计费单价)
        unitFixList: [], //单位(固定金额)
        isEdit: true,
        otherCashDialog: false,
        otherCashList: [
          { key: 'guarantee', label: '履约保证金', checked: false },
          { key: 'other', label: '其他费用', checked: false },
        ],
        checkOtherList: [],
        currentProjectKey: '',
        selfAssetsList: [],
        optionObj: {},
      };
    },

    watch: {
      choseProjectKey: {
        handler(val) {
          this.currentProjectKey = val;
        },
      },
      contractInfo: {
        deep: true,
        handler(obj) {
          if (this.isFirst) {
            if (obj.performanceDepositList && obj.performanceDepositList.length > 0) {
              this.checkOtherList.push('guarantee');
              this.otherCashList[0].checked = true;
            }
            if (obj.otherRentList && obj.otherRentList.length > 0) {
              this.checkOtherList.push('other');
              this.otherCashList[1].checked = true;
            }
          }
          if (!this.$route.query.isAgreement && obj.contractStatus && obj.contractStatus != 3) {
            this.isEdit = false;
          }
        },
      },
    },

    methods: {
      //常量
      async getConstant() {
        let res = await apiConstant();
        this.optionObj = res;
      },

      saveFn() {
        let resArr = [];
        let resObj = {};
        for (let i in this.$refs) {
          let result = this.$refs[i].saveFn();
          let key = Object.keys(result)[0];
          let value = Object.values(result)[0];
          // resArr.push(this.$refs[i].saveFn());
          if (!value) {
            return false;
          }
          resObj[key] = value;
        }
        // if (resArr.indexOf(false) > -1) {
        //   this.$message.warning('合同约定费用全为必填项，请检查！');
        //   return false;
        // }
        return resObj;
      },

      //添加其他非周期性付款费用项
      addOtherCash() {
        this.otherCashDialog = true;
      },

      //添加其他非周期性付款费用项确认
      confirmOtherCash() {
        this.checkOtherList = [];
        this.otherCashList.forEach(item => {
          if (item.checked) {
            this.checkOtherList.push(item.key);
          }
        });
        this.otherCashDialog = false;
      },

      //得到自己添加的资产信息list
      getSelfAssetsList(list) {
        let _list = JSON.parse(JSON.stringify(list));
        _list = lodash.uniqBy(_list, 'assetKey');
        this.selfAssetsList = _list;
      },
    },

    mounted() {
      this.getConstant();
    },

    components: { rentTable, assetTable, discountTable, guaranteeTable, otherCostTable },
  };
</script>
<style lang="scss" scoped>
  ::v-deep .el-collapse-item__header {
    font-weight: 700;
    border: none;
  }
</style>
